// pages/healthManage/dealManage/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      id:"",
      allData:[],
      code:"",
      pmSystem:"",
      checkItem:"",
      content:"",
      units:"",
      iosDialog1:false,
      min:"",
      nom:"",
      juge:true,
      resultList:[],
      num:0,
      workOrderCode:"",
      max:"",
      resultRecord:"",
      syy:false,
      xyy:false,
      content:"",
      result:"",
      category:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const eventChannel = this.getOpenerEventChannel();
		// 监听 index页面定义的toHome 事件
		eventChannel.on('toDeal', (res) => {
      console.log(res,'页面传值') 
      this.setData({
        code:res.code,
        id:res.id,
        workOrderCode:res.workOrderCode
      })
      this.initData()
    })
   
  },
  //关闭弹窗
  close: function () {
    this.setData({
        iosDialog1: false,
    })
},
// sureData(){
//   let self = this;
//   let obj = { list: self.data.allData, informationId: self.data.id, type: 1 };
//   let param = { code: self.data.code, workOrderCode: self.data.workOrderCode };
//   self
//   .axios
//   .add("HealthControlExecute", obj, false)
//   .then(() => {
//     wx.showToast({
//       title: '保存成功',
//       icon: 'succes',
//       duration: 1000,
//       mask:true
//   })
//   this.setData({
//     iosDialog1: false,
// })
//     wx.navigateTo({
//       url: '../report/index',
//           success:function(res){
//            res.eventChannel.emit('toreport',param)
//     }
//    })
//   })
//   .catch((e) => {
//     self.$notify(e.data.msg, "warning");
//   });
// },

//暂存前检查是否超阈值
checkreport() {
  let self = this;
  if(self.data.isresult!=true){
    let data = self.data.allData[self.data.num].result
    let patt=new RegExp("^(-{0,1}[0-9])+(.[0-9]{1,})?$");
    let juge=""
      juge = patt.test(data)
      if(!juge){
      self.showModal({msg:'请填写正确检查结果'})
      return
      }
  }
  let checkObj={list: self.data.allData,code: self.data.code, workOrderCode: self.data.workOrderCode,type:1,informationId: self.data.id}
  self
    .axios
    .custom("HealthControlRecordCheck", "checkRecord", checkObj)
    .then(function (response) {
      console.log(response)
     if(response.data.isOut==0){
        self.saveData()
     }else{
       let content = response.data.content
       content = content.split(";")
        self.setData({
          iosDialog1:true,
          content:content
        })
     }
    });

},
//暂存
  saveData() {
    let self = this;
    let obj = { list: self.data.allData, informationId: self.data.id, type: 0 };
      self
        .axios
        .add("HealthControlExecute", obj, false)
        .then(() => {
          wx.showToast({
            title: '暂存成功',
            icon: 'succes',
            duration: 2000,
            success:()=>{
              self.setData({
                iosDialog1:false
              })
            }
        })
      //   wx.navigateBack({
      //     delta: 2
      // })
          // this.$emit("confirm");
        })
        .catch((e) => {
          self.$notify(e.msg, "warning");
        });
 
  },
  //初始化
  initData(){
    let self = this;
    self
      .axios
      .query("HealthControlInformation", {}, { id: self.data.id }, "", false)
      .then((res) => {
        if(res.data.rows[0].resultList){
          res.data.rows[0].resultList.forEach(e=>{
            e.result = e.resultRecord
          })
          self.data.allData = res.data.rows[0].resultList
          self.setData({
            allData:self.data.allData
          })
          console.log(res);
          if(res.data.rows[0].resultList[0].resultList){
            self.setData({
              isresult:true,
              resultList:res.data.rows[0].resultList[0].resultList
            })
          }else{
            self.setData({
              isresult:false
            })
          }
          if(res.data.rows[0].resultList[0].tabName==0){
            self.setData({
              checkItem:res.data.rows[0].resultList[0].checkItem,
              category:res.data.rows[0].resultList[0].category,
              syy:true,
              result:res.data.rows[0].resultList[0].resultRecord?res.data.rows[0].resultList[0].resultRecord:"",
              juge:false
            })
          }else{
            self.setData({
       
              pmSystem:res.data.rows[0].resultList[0].pmSystem,
              checkItem:res.data.rows[0].resultList[0].checkItem,
              units:res.data.rows[0].resultList[0].units,
              min:res.data.rows[0].resultList[0].min,
              syy:true,
              nom:res.data.rows[0].resultList[0].nom,
              max:res.data.rows[0].resultList[0].max,
              result:res.data.rows[0].resultList[0].resultRecord?res.data.rows[0].resultList[0].resultRecord:"",
              category:res.data.rows[0].resultList[0].category,
              juge:true
            })
          }
        }else{
          self.getAllData()
        }
      }).catch(e=>{

      })
  },
  //下一页
  numadd(){
    let self = this
    if(self.data.num==self.data.allData.length-2){
      self.setData({
        xyy:true
      })

    }else{
      self.setData({
        syy:false
      })
    }

    console.log(self.data.allData[self.data.num].result)
    // if(self.data.allData[self.data.num].result==""||self.data.allData[self.data.num].result==null){
    //   self.showModal({msg:'请填写检查结果'})
    //   return
    // }
    if(self.data.isresult!=true){
      let data = self.data.allData[self.data.num].result
      let patt=new RegExp("^(-{0,1}[0-9])+(.[0-9]{1,})?$");
      let juge=""
        juge = patt.test(data)
        if(!juge){
        self.showModal({msg:'请填写正确检查结果'})
        return
        }
    }
    console.log(self.data.result)
    //self.data.allData[self.data.num].result = self.data.result
    self.data.num++
    self.setData({
      num:self.data.num
    })
    if(self.data.allData[self.data.num].resultList){
      self.setData({
        resultList:self.data.allData[self.data.num].resultList,
        isresult:true
      })
    }else{
      self.setData({
        isresult:false
      })
    }
    if(self.data.allData[self.data.num].tabName==0){
      self.setData({
        checkItem:self.data.allData[self.data.num].checkItem,
        category:self.data.allData[self.data.num].category,
        result:self.data.allData[self.data.num].resultRecord?self.data.allData[self.data.num].resultRecord:"",
        juge:false
      })
    }else{
      self.setData({
     
        pmSystem:self.data.allData[self.data.num].pmSystem,
        checkItem:self.data.allData[self.data.num].checkItem,
        units:self.data.allData[self.data.num].units,
        min:self.data.allData[self.data.num].min,
        nom:self.data.allData[self.data.num].nom,
        result:self.data.allData[self.data.num].resultRecord?self.data.allData[self.data.num].resultRecord:"",
        max:self.data.allData[self.data.num].max,
        category:self.data.allData[self.data.num].category,
        juge:true,
      })
    }
    
  },
        // 展示信息
        showModal(error){
          wx.showModal({
            content: error.msg,
            showCancel: false,
          })
        },
    bindPicker3Change: function(e) {
      let self = this
      this.setData({
          result: this.data.resultList[e.detail.value],
      })
      self.data.allData[self.data.num].result = this.data.resultList[e.detail.value]
      self.data.allData[self.data.num].resultRecord=this.data.resultList[e.detail.value]
  },
  //上一页
  numless(){
    let self = this
    if(self.data.num==1){
      self.setData({
        syy:true
      })
    }else{
      self.setData({
        xyy:false
      })
    }
    //self.data.allData[self.data.num].result = self.data.result
    self.data.num--
    self.setData({
      num:self.data.num
    })
    if(self.data.allData[self.data.num].resultList){
      self.setData({
        resultList:self.data.allData[self.data.num].resultList,
        isresult:true
      })
    }else{
      self.setData({
        isresult:false
      })
    }
    if(self.data.allData[self.data.num].tabName==0){
      self.setData({
        checkItem:self.data.allData[self.data.num].checkItem,
        category:self.data.allData[self.data.num].category,
        result:self.data.allData[self.data.num].resultRecord?self.data.allData[self.data.num].resultRecord:"",
        juge:false
      })
    }else{
      self.setData({
        pmSystem:self.data.allData[self.data.num].pmSystem,
        checkItem:self.data.allData[self.data.num].checkItem,
        units:self.data.allData[self.data.num].units,
        min:self.data.allData[self.data.num].min,
        result:self.data.allData[self.data.num].resultRecord?self.data.allData[self.data.num].resultRecord:"",
        nom:self.data.allData[self.data.num].nom,
        max:self.data.allData[self.data.num].max,
        category:self.data.allData[self.data.num].category,
        juge:true
      })
    }
  },
  resulchange(e){
    let self =this
    this.setData({
      result:e.detail.value,
    })
    self.data.allData[self.data.num].result = e.detail.value
    self.data.allData[self.data.num].resultRecord=e.detail.value
  },
getAllData(){
  let self = this
  self
  .axios
  .query(
    "HealthControlCheckInput",
    {},
    { code: self.data.code, informationId: self.data.id },
    "",
    false
  )
  .then((res) => {
    self.data.allData = res.data.rows
    self.setData({
      allData:self.data.allData
    })
    console.log(res);
    if(res.data.rows[0].resultList){
      self.setData({
        isresult:true,
        syy:true,
        resultList:res.data.rows[0].resultList
      })
    }else{
      self.setData({
        isresult:false,
        syy:true,
      })
    }
    if(res.data.rows[0].tabName==0){
      self.setData({
        checkItem:res.data.rows[0].checkItem,
        category:res.data.rows[0].category,
        juge:false
      })
    }else{
      self.setData({
   
        pmSystem:res.data.rows[0].pmSystem,
        checkItem:res.data.rows[0].checkItem,
        units:res.data.rows[0].units,
        min:res.data.rows[0].min,
        nom:res.data.rows[0].nom,
        max:res.data.rows[0].max,
        category:res.data.rows[0].category,
        juge:true
      })
    }

  })
  .catch((e) => {
    console.log(e);
  });
},
report() {
  let self = this;
  let param = { code: self.data.code, workOrderCode: self.data.workOrderCode };
  let obj = { list: self.data.allData, informationId: self.data.id, type: 1 };
  console.log(self.data.code)
  let checkObj={list: self.data.allData,code: self.data.code, workOrderCode: self.data.workOrderCode,type:1,informationId: self.data.id}
  self
    .axios
    .custom("HealthControlRecordCheck", "checkRecord", checkObj)
    .then(function (response) {
      console.log(response)
     if(response.data.isOut==0){
        self
          .axios
          .add("HealthControlExecute", obj, false)
          .then(() => {
            wx.showToast({
              title: '保存成功',
              icon: 'succes',
              duration: 1000,
              mask:true
          })
            wx.navigateTo({
              url: '../report/index',
                  success:function(res){
                   res.eventChannel.emit('toreport',param)
            }
           })
          })
          .catch((e) => {
            self.$notify(e.data.msg, "warning");
          });
     }else{
       let content=""
       content=response.data.content.split(";")
       content.pop()
       console.log(content)
       self.setData({
         iosDialog1:true,
         content:content
       })
     }
    });

},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})